home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / CSharp / Graphics / HexView / PictureHEXView.cs < prev    next >
Encoding:
Text File  |  2004-10-22  |  8.5 KB  |  234 lines

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.IO;
  8.  
  9. /*
  10.    PictureHEXView is a simple picture and color-in-hexa viewer
  11.    which tracks pixel values under the cursor.
  12.    Demonstrates use of OpenFileDialog for loading
  13.    image files and Type-overcasting for get color codes for a pixel.
  14.    Made by Endre I. Simay, Hungary
  15. */
  16.  
  17. namespace PictureHEXView {
  18.     /// <summary>
  19.     /// Summary description for WinForm.
  20.     /// </summary>
  21. public class WinForm : System.Windows.Forms.Form {
  22.         /// <summary>
  23.         /// Required designer variable.
  24.         /// </summary>
  25.         private System.ComponentModel.Container components = null;
  26.         private System.Windows.Forms.Panel panel1;
  27.         private System.Windows.Forms.PictureBox pictureBox1;
  28.         private System.Windows.Forms.Button button1;
  29.         private System.Windows.Forms.OpenFileDialog openFileDialog1;
  30.  
  31.         System.Drawing.Color color = new System.Drawing.Color();
  32.  
  33.         private System.Windows.Forms.Label label1;
  34.         private System.Windows.Forms.Label label2;
  35.         private System.Windows.Forms.Label label3;
  36.         public WinForm() {
  37.             //
  38.             // Required for Windows Form Designer support
  39.             //
  40.             InitializeComponent();
  41.  
  42.             //
  43.             // TODO: Add any constructor code after InitializeComponent call
  44.             //
  45.         }
  46.  
  47.         /// <summary>
  48.         /// Clean up any resources being used.
  49.         /// </summary>
  50.         protected override void Dispose (bool disposing) {
  51.             if (disposing) {
  52.                 if (components != null) {
  53.                     components.Dispose();
  54.                 }
  55.             }
  56.             base.Dispose(disposing);
  57.         }
  58.  
  59.         #region Windows Form Designer generated code
  60.         /// <summary>
  61.         /// Required method for Designer support - do not modify
  62.         /// the contents of this method with the code editor.
  63.         /// </summary>
  64.         private void InitializeComponent()
  65.         {
  66.             this.panel1 = new System.Windows.Forms.Panel();
  67.             this.pictureBox1 = new System.Windows.Forms.PictureBox();
  68.             this.button1 = new System.Windows.Forms.Button();
  69.             this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
  70.             this.label1 = new System.Windows.Forms.Label();
  71.             this.label2 = new System.Windows.Forms.Label();
  72.             this.label3 = new System.Windows.Forms.Label();
  73.             this.panel1.SuspendLayout();
  74.             this.SuspendLayout();
  75.             // 
  76.             // panel1
  77.             // 
  78.             this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  79.                         | System.Windows.Forms.AnchorStyles.Left) 
  80.                         | System.Windows.Forms.AnchorStyles.Right)));
  81.             this.panel1.AutoScroll = true;
  82.             this.panel1.Controls.Add(this.pictureBox1);
  83.             this.panel1.Location = new System.Drawing.Point(8, 8);
  84.             this.panel1.Name = "panel1";
  85.             this.panel1.Size = new System.Drawing.Size(360, 232);
  86.             this.panel1.TabIndex = 0;
  87.             // 
  88.             // pictureBox1
  89.             // 
  90.             this.pictureBox1.Location = new System.Drawing.Point(0, 0);
  91.             this.pictureBox1.Name = "pictureBox1";
  92.             this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
  93.             this.pictureBox1.TabIndex = 0;
  94.             this.pictureBox1.TabStop = false;
  95.             this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
  96.             // 
  97.             // button1
  98.             // 
  99.             this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  100.             this.button1.Location = new System.Drawing.Point(8, 248);
  101.             this.button1.Name = "button1";
  102.             this.button1.Size = new System.Drawing.Size(88, 23);
  103.             this.button1.TabIndex = 1;
  104.             this.button1.Text = "Load Image ...";
  105.             this.button1.Click += new System.EventHandler(this.button1_Click);
  106.             // 
  107.             // openFileDialog1
  108.             // 
  109.             this.openFileDialog1.Filter = "All known (*.bmp;*.gif;*.ico;*.jpg;*.png)|*.bmp;*.gif;*.ico;*.jpg;" +  
  110.                 "*.png";
  111.             // 
  112.             // label1
  113.             // 
  114.             this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  115.             this.label1.ForeColor = System.Drawing.Color.FromArgb(((byte)(192)), ((byte)(0)), ((byte)(0)));
  116.             this.label1.Location = new System.Drawing.Point(112, 256);
  117.             this.label1.Name = "label1";
  118.             this.label1.Size = new System.Drawing.Size(72, 16);
  119.             this.label1.TabIndex = 2;
  120.             this.label1.Text = "Red  :";
  121.             // 
  122.             // label2
  123.             // 
  124.             this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  125.             this.label2.ForeColor = System.Drawing.Color.FromArgb(((byte)(0)), ((byte)(192)), ((byte)(0)));
  126.             this.label2.Location = new System.Drawing.Point(200, 256);
  127.             this.label2.Name = "label2";
  128.             this.label2.Size = new System.Drawing.Size(72, 16);
  129.             this.label2.TabIndex = 3;
  130.             this.label2.Text = "Green :";
  131.             // 
  132.             // label3
  133.             // 
  134.             this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  135.             this.label3.ForeColor = System.Drawing.Color.FromArgb(((byte)(0)), ((byte)(0)), ((byte)(192)));
  136.             this.label3.Location = new System.Drawing.Point(288, 256);
  137.             this.label3.Name = "label3";
  138.             this.label3.Size = new System.Drawing.Size(72, 16);
  139.             this.label3.TabIndex = 4;
  140.             this.label3.Text = "Blue :";
  141.             // 
  142.             // WinForm
  143.             // 
  144.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  145.             this.ClientSize = new System.Drawing.Size(377, 286);
  146.             this.Controls.Add(this.label3);
  147.             this.Controls.Add(this.label2);
  148.             this.Controls.Add(this.label1);
  149.             this.Controls.Add(this.button1);
  150.             this.Controls.Add(this.panel1);
  151.             this.MinimumSize = new System.Drawing.Size(385, 320);
  152.             this.Name = "WinForm";
  153.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  154.             this.Text = "PictureHexView";
  155.             this.panel1.ResumeLayout(false);
  156.             this.ResumeLayout(false);
  157.         }
  158.         #endregion
  159.  
  160.         /// <summary>
  161.         /// The main entry point for the application.
  162.         /// </summary>
  163.         [STAThread]
  164.         static void Main() {
  165.             Application.Run(new WinForm());
  166.         }
  167.  
  168.  
  169.         private bool open_and_view() {
  170.             string fname;
  171.             if ((fname = openFileDialog1.FileName)!= "") {
  172.                 try {
  173.                     try {
  174.                         if (pictureBox1.Image!= null) {
  175.                             pictureBox1.Image.Dispose();
  176.                         }
  177.                         pictureBox1.Image = Image.FromFile(fname);
  178.                     } catch (Exception) {
  179.                         return false;
  180.                     }
  181.                     return true;
  182.                 } catch (Exception) {
  183.                     return false;
  184.                 }
  185.             } else {
  186.                 return false;
  187.             }
  188.         }
  189.  
  190.         private void button1_Click(object sender, System.EventArgs e) {
  191.             if (openFileDialog1.ShowDialog() == DialogResult.OK) {
  192.                 if (!open_and_view()) {
  193.                     // a simple error-recording
  194.  
  195.                     MessageBox.Show("Problem with picture path or format." + "Sorry", "Pictureview Error",
  196.                                     MessageBoxButtons.OK);
  197.                 }
  198.             }
  199.         }
  200.  
  201.         string stemp;
  202.  
  203.         private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) {
  204.  
  205.             if (pictureBox1.Image != null) {
  206.                 // The Image-type has not a GetPixel function. So we use the Bitmap's one
  207.                 color = ((System.Drawing.Bitmap)pictureBox1.Image).GetPixel(e.X,e.Y);
  208.  
  209.                 // A very stone-axe method to padding the byte to hexa with 2 chars
  210.                 stemp = color.R.ToString("X");
  211.                 if (stemp.Length == 1) {
  212.                     stemp = "0"+ stemp;
  213.                 }
  214.                 label1.Text = "Red  : 0x" + stemp;
  215.  
  216.                 stemp = color.G.ToString("X");
  217.                 if (stemp.Length == 1) {
  218.                     stemp = "0"+ stemp;
  219.                 }
  220.                 label2.Text = "Green: 0x" + stemp;
  221.  
  222.                 stemp = color.B.ToString("X");
  223.                 if (stemp.Length == 1) {
  224.                     stemp = "0"+ stemp;
  225.                 }
  226.                 label3.Text = "Blue : 0x" + stemp;
  227.             }
  228.         }
  229.  
  230.     }
  231. }
  232.  
  233.  
  234.